package fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.boltshow.R;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;

import application.MyApplication;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import model.User;

/**
 * Created by Administrator on 2016/8/30.
 */
public class LoginFragment extends Fragment implements CompoundButton.OnCheckedChangeListener, PlatformActionListener, View.OnClickListener {

    private EditText mETUser,mETPassword;
    private CheckBox mCheckBoxEye;
    private DbManager mDb;
    private MyApplication mApp;
    private ImageView ivQQ,ivWeiBo;
    private Button mButtonLogin;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_fragment,null);

        initView(view);
        initData();
        setData();
        setListener();

        return view;
    }

    private void setListener() {
        ivQQ.setOnClickListener(this);
        ivWeiBo.setOnClickListener(this);
        mButtonLogin.setOnClickListener(this);
    }

    private void setData() {

    }

    private void initData() {
        mCheckBoxEye.setOnCheckedChangeListener(this);

        DbManager.DaoConfig config = new DbManager.DaoConfig();
        mDb = x.getDb(config);
        mApp = (MyApplication) getActivity().getApplication();
    }

    private void initView(View view) {
        mETUser = (EditText) view.findViewById(R.id.et_login_user);
        mETPassword = (EditText) view.findViewById(R.id.et_login_password);
        mCheckBoxEye = (CheckBox) view.findViewById(R.id.cb_login_eye);
        ivQQ = (ImageView) view.findViewById(R.id.iv_login_qq);
        ivWeiBo = (ImageView) view.findViewById(R.id.iv_login_weibo);
        mButtonLogin = (Button) view.findViewById(R.id.bt_login_login);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_login_eye:
                if (isChecked) {
                    mETPassword.setInputType(EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    mETPassword.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD
                            | EditorInfo.TYPE_CLASS_TEXT);

                }
                break;

            default:
                break;
        }
    }

    private void authorize(Platform plat) {
        if (plat == null) {
            // popupOthers();
            Toast.makeText(getActivity(), "平台为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //判断指定平台是否已经完成授权
        if (plat.isAuthValid()) {
            String userName = plat.getDb().getUserName();
            if (userName != null) {
                try {
                    List<User> usernameList = mDb.selector(User.class).where("username", "=", userName).findAll();
                    if(usernameList.size()>0) {
                        mApp.setLogin(true);
                        mApp.setUserName(usernameList.get(0).getUsername());
                        mApp.setUserIcon(usernameList.get(0).getUserIcon());
                        Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                        getActivity().finish();
                    }

                } catch (DbException e) {
                    e.printStackTrace();
                }

            }

            return;
        }
        plat.setPlatformActionListener(this);
        // true不使用SSO授权，false使用SSO授权
        plat.SSOSetting(false);
        //获取用户资料
        plat.showUser(null);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        String userName = platform.getDb().getUserName();
        String userIcon = platform.getDb().getUserIcon();
        User user = new User();
        user.setUsername(userName);
        user.setUserIcon(userIcon);
        try {
            mDb.save(user);
            mApp.setUserName(userName);
            mApp.setLogin(true);
            mApp.setUserIcon(userIcon);
            Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
            getActivity().finish();
        } catch (DbException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {

    }

    @Override
    public void onCancel(Platform platform, int i) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_login_qq :
                ShareSDK.initSDK(getActivity());
                Platform platform2 = ShareSDK.getPlatform(QQ.NAME);
                authorize(platform2);
                break;
            case R.id.iv_login_weibo :
                ShareSDK.initSDK(getActivity());
                Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
                authorize(platform);
                break;
            case R.id.bt_login_login:
                String userNumber = mETUser.getText().toString();
                String passWord = mETPassword.getText().toString();
                if(!TextUtils.isEmpty(userNumber)&&!TextUtils.isEmpty(passWord)) {
                    try {
                        List<User> userList = mDb.selector(User.class).where("userNumber", "=", userNumber).findAll();
                        if(userList!=null&&userList.size()>0&&userList.get(0).getUserNumber().equals(userNumber)&&userList.get(0).getPassword().equals(passWord)) {
                            Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                            mApp.setLogin(true);
                            mApp.setUserName(userList.get(0).getUserNumber());
                            getActivity().finish();
                        }
                        else {
                            Toast.makeText(getActivity(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    } catch (DbException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(getActivity(), "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                }

                break;

        }
    }
}
